import os
from csst_dfs_api.common.catalog import CatalogApi


def gaia_query_from_dfs(ra=180, dec=0, radius=2, min_mag=0, max_mag=20):
    """ query gaia edr3 catalog from dfs """
    os.environ["CSST_DFS_API_MODE"] = "cluster"
    os.environ["CSST_DFS_GATEWAY"] = "222.197.214.168:30880"
    os.environ["CSST_DFS_APP_ID"] = "1"
    os.environ["CSST_DFS_APP_TOKEN"] = "1"

    api = CatalogApi()
    cat = api.catalog_query(
        catalog_name="gaia3", ra=ra, dec=dec, radius=radius, min_mag=min_mag, max_mag=max_mag, obstime=-1, limit=-1)
    tbl = api.to_table(cat)
    try:
        assert len(tbl) > 0
    except AssertionError:
        raise AssertionError("The query returns a zero-row table!")
    return tbl


if __name__ == "__main__":
    tbl = gaia_query_from_dfs()
    print(tbl)
